package br.com.healthy.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.text.IconView;

import br.com.healthy.model.entity.Address;
import br.com.healthy.model.entity.Contact;
import br.com.healthy.model.entity.Patient;
import br.com.healthy.model.entity.SexType;
import br.com.healthy.model.service.PatientService;
import br.com.healthy.view.FormPatient;
/**
 * 
 * @author Dieb
 *
 */

public class PatienActionListener implements ActionListener {

	private FormPatient form;
	private PatientService service;
	
	public PatienActionListener(FormPatient formPatient){
		
		this.form = formPatient;
		service = new PatientService();
		attachListener();
	}

	public void attachListener(){
		form.getBtnInclude().addActionListener(this);
		form.getBtnAlter().addActionListener(this);
		form.getBtnExclude().addActionListener(this);
		form.getBtnSave().addActionListener(this);
		form.getBtnCancel().addActionListener(this);
	}
	
	private void enableButtonsToSaveAction(){
		enableOrDisableButtonsToEdit(true);
	}
	
	private void disableButtonsToSaveAction(){
		enableOrDisableButtonsToEdit(false);
	}
	
	private void enableOrDisableButtonsToEdit(Boolean enabled){
		
		form.getBtnInclude().setEnabled(!enabled);
		form.getBtnAlter().setEnabled(!enabled);
		form.getBtnExclude().setEnabled(!enabled);
		form.getBtnSave().setEnabled(enabled);
		form.getBtnCancel().setEnabled(enabled);
	}
	
	private void cleanFormEdit(){
		
		form.getTextName().setText("");
		form.getTextRg().setText("");
		form.getTextCpf().setText("");
		form.getComboSex().setSelectedIndex(0);
		form.getTextEmail().setText("");
		form.getTextPhone().setText("");
		form.getTextCell().setText("");
		form.getTextFax().setText("");
		form.getTextAddress().setText("");
		form.getTextCity().setText("");
		form.getTextDistrict().setText("");
		form.getTextZipCode().setText("");
	}
	
	private void enableOrDisablePatientEdit(Boolean enabled){
		
		form.getTextName().setEnabled(enabled);
		form.getTextRg().setEnabled(enabled);
		form.getTextCpf().setEnabled(enabled);
		form.getComboSex().setEnabled(enabled);
		form.getTextEmail().setEnabled(enabled);
		form.getTextPhone().setEnabled(enabled);
		form.getTextCell().setEnabled(enabled);
		form.getTextFax().setEnabled(enabled);
		form.getTextAddress().setEnabled(enabled);
		form.getTextCity().setEnabled(enabled);
		form.getTextDistrict().setEnabled(enabled);
		form.getTextZipCode().setEnabled(enabled);
		
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {

		if(event.getActionCommand().equals("Incluir")){
			Insert();
		}else if(event.getActionCommand().equals("Alterar")){
			
		}else if(event.getActionCommand().equals("Excluir")){
			
		}else if(event.getActionCommand().equals("Salvar")){
			save();
		}else if(event.getActionCommand().equals("Cancelar")){
			Cancel();
		};
		
	}
	
	private void Insert(){
		enableButtonsToSaveAction();
		enableOrDisablePatientEdit(true);
	}
	
	private void save(){
		
		Icon figura = new ImageIcon ("images/success32.png"); 
		
		service.Insert(formToPatient());
		JOptionPane.showMessageDialog(form, "Paciente Cadastrado", "Cadastro",JOptionPane.INFORMATION_MESSAGE,figura );
		disableButtonsToSaveAction();
		cleanFormEdit();
		enableOrDisablePatientEdit(false);
		
	}
	
	private void Cancel(){
		disableButtonsToSaveAction();
		cleanFormEdit();
		enableOrDisablePatientEdit(false);
	}
	
	private Patient formToPatient(){
		
		SexType sex = null;
		String comboSex = form.getComboSex().getSelectedItem().toString();
		Patient patient = new Patient();
		patient.setName(form.getTextName().getText());
		patient.setRg(form.getTextRg().getText());
		patient.setCpf(form.getTextCpf().getText());
		
		if (comboSex.equals("Masculino")){
			patient.setSex(sex.M);
		}else patient.setSex(sex.F);
		
		Address address = new Address();
		address.setAddress(form.getTextAddress().getText());
		address.setCity(form.getTextDistrict().getText());
		address.setCity(form.getTextCity().getText());
		address.setZipCode(form.getTextZipCode().getText());
		
		Contact contact = new Contact();
		contact.setEmail(form.getTextEmail().getText());
		contact.setPhone(form.getTextPhone().getText());
		contact.setCellPhone(form.getTextCell().getText());
		contact.setFax(form.getTextFax().getText());
		
		patient.setAddress(address);
		patient.setContact(contact);
				
		return patient;
	}

}
